﻿

(function () {
    appModule.controller('common.views.projectStatuss.createOrEditModal', [
        '$scope', '$uibModalInstance', 'abp.services.app.projectStatus', 'projectStatusId', 'FileUploader', 'projectStatus', 'abp.services.app.area', 'abp.services.app.file', 'textAngularManager',
        function ($scope, $uibModalInstance, projectStatusService, projectStatusId, fileUploader, projectStatus, areaService, fileService, textAngularManager) {
            var vm = this;
            vm.saving = false;
            //首先将projectStatus数据设置为null
            vm.projectStatus = null;

            //上传方法
            vm.uploader = new fileUploader({
                url: abp.appPath + 'Profile/UploadFile',
                headers: {
                    "X-XSRF-TOKEN": abp.security.antiForgery.getToken()
                },
                queueLimit: 1,
                autoUpload: true,
                removeAfterUpload: true,
                filters: [{
                    name: 'imageFilter',
                    fn: function (item, options) {

                        //File size check
                        if (item.size > 4194304) //4MB
                        {
                            abp.message.warn(app.localize('File_Warn_FileType'));
                            return false;
                        }

                        return true;
                    }
                }]
            });

            vm.uploader.onSuccessItem = function (fileItem, response, status, headers) {
                if (response.success) {
                    vm.fileTitle = response.result.fileName;
                    vm.fileUrl = response.result.filePath;
                    vm.projectStatus.fileID = response.result.fileID;

                } else {
                    abp.message.error(response.error.message);
                }
            };

            //文件下载方法
            vm.downFile = function (filePath) {
                window.location.target = '_blank';
                window.location.href = filePath;
            }

            vm.fileClick = function () {
                $("#FileID").click();
            };


            //触发保存方法
            vm.save = function () {
                vm.saving = true;
                projectStatusService.createOrUpdateProjectStatusAsync({ projectStatusEditDto: vm.projectStatus }).then(function () {
                    abp.notify.info(app.localize('SavedSuccessfully'));
                    $uibModalInstance.close();
                }).finally(function () {
                    vm.saving = false;
                });


            };
            //取消关闭页面
            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };

            //初始化页面
            function init() {

                //绑定镇控件
                areaService.getAreaByLevelAsync({ id: 0 }).then(function (result) {
                    vm.townList = result.data.items;
                });

                if (projectStatusId != null) {

                    //绑定区控件
                    areaService.getAreaByLevelAsync({ id: projectStatus.townID }).then(function (result) {
                        vm.areaList = result.data.items;
                    });

                    //绑定村控件
                    areaService.getAreaByLevelAsync({ id: projectStatus.areaID }).then(function (result) {
                        vm.villageList = result.data.items;

                        //绑定数据
                        projectStatusService.getProjectStatusForEditAsync({
                            id: projectStatusId
                        }).then(function (result) {
                            vm.projectStatus = result.data.projectStatus;

                            //绑定文件数据
                            fileService.getFileForEditAsync({
                                id: vm.projectStatus.fileID
                            }).then(function (result) {
                                vm.fileTitle = result.data.file.title;
                                vm.fileUrl = result.data.file.url;
                            });

                        });

                    });

                }
                else {
                    projectStatusService.getProjectStatusForEditAsync({
                        id: projectStatusId
                    }).then(function (result) {

                        vm.projectStatus = result.data.projectStatus;
                    });
                }
            }

            //绑定区
            vm.bindArea = function (townID) {
                areaService.getAreaByLevelAsync({ id: townID }).then(function (result) {
                    vm.areaList = result.data.items;
                });
            };

            //绑定村
            vm.bindVillage = function (areaID) {
                areaService.getAreaByLevelAsync({ id: areaID }).then(function (result) {
                    vm.villageList = result.data.items;
                });
            };

            //执行初始化方法
            init();
        }
    ]);
})();